﻿using System;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay;
using LFloatMath.Math;

namespace IQIGame.Onigao.Logic
{
    public class SkillEffective_HpToShield : SkillEffective
    {
        protected override ISerializeCustomPOD OnExecute(SkillEffectiveData data, Unit target, SkillEffectiveContext context, int effectiveIndex, out SkillEffectiveResponse startResp)
        {
            startResp = null;
            if (data.effectiveConfig.customConfig is SkillEffective_HpToShield_ConfigData rConfig)
            {
                var nHpFormula = rConfig.hpFormula;
                var fShieldRate = new LFloat(true, rConfig.shieldRate);
                
                var rAttr = target.GetComponent<AttributeComponent>(ETComponentType.AttributeComponent);
                var fMaxHp = rAttr.Get(EAttributeType.MaxHp);
                var fHp = rAttr.Get(EAttributeType.Hp);
                var nValue = LMath.Clamp(Formula.Calculate<LFloat>(nHpFormula, context.skill.executer, target), LFloat.zero, fHp - 1).ToInt();
                DamageController.RemoveHpWithoutDamage(target, nValue);

                var rBuffComp = target.GetComponent<BuffComponent>(ETComponentType.BuffComponent);
                var nShieldValue = (nValue * fShieldRate).ToInt();
                var rAttacker = context.skill.executer;
                var rBuffHandler = BuffController.Add(rAttacker.GetComponent<BuffComponent>(ETComponentType.BuffComponent), rBuffComp, rConfig.shieldBuffID, param: nShieldValue);
                return BuffData.Create(rBuffHandler);
            }
            
            return null;
        }
    }
}